package com.situ.mall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.situ.mall.api.product.ProductClient;
import com.situ.mall.common.util.Result;
import com.situ.mall.order.pojo.vo.CartVO;
import com.situ.mall.pojo.entity.order.Cart;
import com.situ.mall.order.mapper.CartMapper;
import com.situ.mall.order.service.ICartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.mall.pojo.entity.product.Product;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author Sun Huijuan
 * @since 2024-10-14
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {
	@Autowired
	private CartMapper cartMapper;
	@Autowired
	private ProductClient productClient;
	
	@Override
	public Result<Cart> add1(Cart cart) {
		//cart里面现在有userId，productId，quantity
		QueryWrapper<Cart> cartqueryWrapper = new QueryWrapper<>();
		cartqueryWrapper.eq("user_id",cart.getUserId());
		cartqueryWrapper.eq("product_id",cart.getProductId());
		Cart cart1=cartMapper.selectOne(cartqueryWrapper);
		if(cart1!=null){
			//1.这个用户userId的购物车里面已经有了这个商品productId
			//更新quantity
			//当更新的时候Result的code为002
			cart.setQuantity(cart.getQuantity()+cart1.getQuantity());
			cartMapper.updateById(cart1);
			Result<Cart> result=new Result<>(2,"添加成功！",cartMapper.selectOne(cartqueryWrapper));
			System.out.println("========="+cartMapper.selectOne(cartqueryWrapper));
			return result;
		}else {
			//2.这个用户没有添加这个商品
			cartMapper.insert(cart);
			Result<Cart> result=new Result<>(0,"添加成功！",cartMapper.selectOne(cartqueryWrapper));
			return result;
			
		}
	}
	
	@Override
	public CartVO add(Cart cart) {
		cartMapper.insert(cart);
		cart = cartMapper.selectById(cart.getId());
		CartVO cartVO = new CartVO();
		BeanUtils.copyProperties(cart, cartVO);
		Result<Product> result = productClient.selectById(cart.getProductId());
		if (result != null) {
			Product product = result.getData();
			cartVO.setProductImage(product.getImage());
			cartVO.setProductName(product.getName());
			cartVO.setProductPrice(product.getPrice());
		}
		return cartVO;
	}
	
	@Override
	public CartVO update(Cart cart) {
		UpdateWrapper<Cart> updateWrapper=new UpdateWrapper<Cart>();
		updateWrapper.eq("user_id",cart.getUserId());
		updateWrapper.eq("product_id",cart.getProductId());
		updateWrapper.set("quantity",cart.getQuantity());
		cartMapper.update(cart,updateWrapper);
		cart = cartMapper.selectById(cart.getId());
		CartVO cartVO = new CartVO();
		BeanUtils.copyProperties(cart, cartVO);
		Result<Product> result = productClient.selectById(cart.getProductId());
		if (result != null) {
			Product product = result.getData();
			cartVO.setProductImage(product.getImage());
			cartVO.setProductName(product.getName());
			cartVO.setProductPrice(product.getPrice());
		}
		return cartVO;
	}
	
	@Override
	public List<CartVO> selectByUserId(Long userId) {
		QueryWrapper<Cart> cartqueryWrapper = new QueryWrapper<>();
		cartqueryWrapper.eq("user_id",userId);
		List<Cart> cartList=cartMapper.selectList(cartqueryWrapper);
		List<CartVO> cartVOList=new ArrayList<>();
		for(Cart cart:cartList){
			CartVO cartVO=new CartVO();
			BeanUtils.copyProperties(cart,cartVO);
			Result<Product> result=productClient.selectById(cart.getProductId());
			if(result.getCode()!=null){
				Product product=result.getData();
				cartVO.setProductImage(product.getImage());
				cartVO.setProductName(product.getName());
				cartVO.setProductPrice(product.getPrice());
			}
			cartVOList.add(cartVO);
		}
		return cartVOList;
	}
}
